package com.lk.web.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lk.web.entity.SysRole;
import com.lk.web.entity.SysUser;
import com.lk.web.mapper.SysRoleMapper;
import com.lk.web.mapper.SysUserMapper;
import com.lk.web.service.SysUserService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户表 服务实现类
 *
 * @author Aspirin
 * @since 2020-06-21
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {
  private static final String PASSWORD_DEFAULT =
          "$2a$10$rDkPvvAFV8kqwvKJzwlRv.i.q.wz1w1pz0SFsHn/55jNeZFQv/eCm";
  @Autowired SysRoleMapper sysRoleMapper;

  @Override
  public SysUser findByUsername(String username) {
    if (StringUtils.isEmpty(username)) {
      return null;
    }
    QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("username", username);
    return baseMapper.selectOne(queryWrapper);
  }

  @Override
  public SysUser findByMobile(String mobile) {
    if (StringUtils.isEmpty(mobile)) {
      return null;
    }
    QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("mobile", mobile);
    return baseMapper.selectOne(queryWrapper);
  }

  @Override
  public IPage<SysUser> selectPage(Page<SysUser> page, SysUser sysUser) {
    return baseMapper.selectPage(page, sysUser);
  }

  @Override
  public SysUser findById(Long id) {
    if (id == null) {
      return new SysUser();
    }
    // 1. 用户id查询用户信息 sys_user
    SysUser sysUser = baseMapper.selectById(id);
    // 2. 用户id查询所拥有的角色
    List<SysRole> roleList = sysRoleMapper.findByUserId(id);
    sysUser.setRoleList(roleList);
    return sysUser;
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public boolean saveOrUpdate(SysUser entity) {
    if (entity != null && entity.getId() == null) {
      // 新增设置默认密码1234
      entity.setPassword(PASSWORD_DEFAULT);
    }
    entity.setUpdateDate(LocalDateTime.now());

    // 1.更新或者保存操作
    boolean flag = super.saveOrUpdate(entity);

    if (flag) {
      // 2. 先删除用户角色表中的数据
      baseMapper.deleteUserRoleByUserId(entity.getId());
      // 3. 再新增到用户角色表中
      if (CollectionUtils.isNotEmpty(entity.getRoleIds())) {
        baseMapper.saveUserRole(entity.getId(), entity.getRoleIds());
      }
    }

    return true;
  }

  @Override
  public boolean deleteById(Long id) {
    // 1. 查询用户信息
    SysUser sysUser = baseMapper.selectById(id);
    // 2. 再更新用户信息，将状态更新为已删除
    sysUser.setEnabled(false);
    sysUser.setUpdateDate(LocalDateTime.now());
    baseMapper.updateById(sysUser);
    return true;
  }
}
